TARGET = sanctiond
EONATG = eon.atg
EONTGT = eon

SRC = timer.cpp combat.cpp threadmgr.cpp newchar.cpp \
	objfactory.cpp world.cpp db.cpp help.cpp main.cpp

OBJDIR = obj
OBJ = $(SRC:%.cpp=%.o)

CPPSTD  = -std=c++0x
CPPDEFS =
LIBDEFS = -lstdc++ -lsanctioned
#LIBDEFS = -lstdc++ -lsocket -lnsl -lsanctioned
#LIBDEFS += -lmysqlclient
LIBDEFS += -lmysqlpp -lmysqlclient
LIBDEFS += -lpthread -lrt -ldl
LIBDIRS = ../lib /usr/local/lib
INCDEFS = 
INCDIRS = ../lib /usr/include/mysql /usr/local/include/mysql++
DEBUG   = 3
OPT	= 0

PROG_BUILD_NUM=.build_number
BUILD_DATE=$(shell date +'%Y%m%d')
BUILD_NUM=$(shell cat $(PROG_BUILD_NUM))

CPPFLAGS = -g$(DEBUG)
CPPFLAGS += $(CPPDEFS)
CPPFLAGS += -O$(OPT)
CPPFLAGS += -DBUILD_DATE=$(BUILD_DATE) -DBUILD_NUM=$(BUILD_NUM)
#CPPFLAGS += -x c++
#CPPFLAGS += -fshort-enums
CPPFLAGS += -funsigned-bitfields
#CPPFLAGS += -fno-exceptions	# -lmysql++
#CPPFLAGS += -fno-rtti	# mysql++
#CPPFLAGS += --ansi --pedantic
CPPFLAGS += -Wall
#CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=%.lst)

#LDDFLAGS = -Wl,-Map=$(TARGET).map,--cref
#LDDFLAGS += -Wl,-relax,--gc-sections
LDDFLAGS = $(patsubst %,-L%,$(LIBDIRS))
LDDFLAGS += $(LIBDEFS)

#LDDFLAGS += $(patsubst %,-R%,$(LIBDIRS))
INCFLAGS += $(patsubst %,-I%,$(INCDIRS))

DEPFLAGS = -MMD -MP -MF .dep/$(@F).d

ALLCPPFLAGS = $(INCFLAGS) $(CPPFLAGS) $(DEPFLAGS)

COCOFLAGS = -F

CC = g++
NM = nm
RM = rm -rf
MV = mv
CP = cp 
COCO = Coco

all: begin $(TARGET) end

begin:
	@echo
	@echo "--------[ Building Government Sanctioned Server ]---------"

end:
	@echo
	@echo "************** FINISHED sanctiond **************"


#	$(COCO) $(NLPATG) -frames $(COCOFRAMEDIR) -o $(NLP) $(COCO_FLAGS)
$(EONTGT): $(EONATG)
	$(MV) eon/Parser.cpp parser.cpp
	$(MV) eon/Parser.h parser.hpp
	$(MV) eon/Scanner.cpp scanner.cpp
	$(MV) eon/Scanner.h scanner.hpp
	$(RM) eon/*.old

$(EONATG):
	$(COCO) eon/$(EONATG) -o eon $(COCO_FLAGS)


.SECONDARY : $(TARGET)
.PRECIOUS : $(OBJ)
$(TARGET): $(OBJ) $(BUILD_NUMBER)
	$(CC) $(OBJ) --output $@ $(LDDFLAGS)

%.o: %.cpp
	$(CC) -c $(ALLCPPFLAGS) $(CPPSTD) $< -o $@

$(BUILD_NUMBER):
	@echo Incrementing build number
	@if ! test -f $(PROG_BUILD_NUM); then echo 0 > $(PROG_BUILD_NUM); fi
	@echo $$(($$(cat $(PROG_BUILD_NUM)) + 1)) > $(PROG_BUILD_NUM)


clean:
	@echo
	@echo "--------[ Cleaning server Directory ]----------"
	$(RM) $(TARGET)
	$(RM) $(TARGET).map
	$(RM) *.lst
	$(RM) $(OBJ)
	$(RM) .dep
	$(RM) *.old
	$(RM) $(EONTGT)/*.cpp
	$(RM) $(EONTGT)/*.h

# Create object files directory
$(shell mkdir $(OBJDIR) 2>/dev/null)

-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)

